﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using ZilLion.Service.WeChatMall.Domain.Entitys.AppInfo;

namespace ZilLion.Service.WeChatMall.Domain.Entitys.ShopInfo
{
    public class ShopInfoDto
    {
        [JsonProperty("shopId")]
        public long ShopId { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("issendcenter")]
        public bool IsSendCenter { get; set; }

        [JsonProperty("shopphone")]
        public string ShopPhone { get; set; }

        [JsonProperty("issincecenter")]
        public bool IsSinceCenter { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("score")]
        public decimal Score { get; set; } = 5;


        [JsonProperty("minPrice")]
        public decimal MinPrice { get; set; }

        [JsonProperty("deliveryPrice")]
        public decimal DeliveryPrice { get; set; }


        [JsonProperty("sellCount")]
        public int SellCount { get; set; }

        [JsonProperty("bulletin")]
        public string Bulletin { get; set; }

        [JsonProperty("supports")]
        public IList<ShopSupportInfoEntity> Supports { get; set; }

        [JsonProperty("avatar")]
        public string Avatar { get; set; }


        [JsonProperty("infos")]
        public IList<string> Infos { get; set; }


        [JsonProperty("location")]
        public string Location { get; set; }


        [JsonProperty("adcode")]
        public string Adcode { get; set; }


        [JsonProperty("gpsaddress")]
        public string GpsAddress { get; set; }

        [JsonProperty("detailaddress")]
        public string DetailAddress { get; set; }


        [JsonProperty("appid")]
        public string ShopAppid { get; set; }

        /// <summary>
        ///     由Dto 拆分成entity
        /// </summary>
        /// <returns></returns>
        public Tuple<ShopBasicInfoEntity, AppShopRelationShipEntity, IList<ShopSupportInfoEntity>>
            SplitToEntity()
        {
            #region 处理

            var basicInfo = new ShopBasicInfoEntity
            {
                ShopId = ShopId,
                ShopAdCode = Adcode,
                ShopAvatar = Avatar,
                ShopBulletin = Bulletin,
                ShopDeliveryPrice = DeliveryPrice,
                ShopDescription = Description,
                ShopDetailAddress = DetailAddress,
                ShopGpsAddress = GpsAddress,
                ShopLocation = Location,
                ShopMinOrderMy = MinPrice,
                ShopName = Name,
                ShopScore = Score,
                ShopInfos = string.Empty,
                IsSendCenter= IsSendCenter,
                IsSinceCenter = IsSinceCenter,
                ShopPhone = ShopPhone ?? string.Empty
            };

            if (Infos != null)
            {
                var infostring = Infos.Aggregate(string.Empty, (current, info) => current + info + ",");

                if (infostring.EndsWith(","))
                    infostring = infostring.Left(infostring.Length - 1);
                basicInfo.ShopInfos = infostring;
            }

            #endregion

            var relationShip = new AppShopRelationShipEntity {AppId = ShopAppid, ShopId = ShopId};
            return new Tuple<ShopBasicInfoEntity, AppShopRelationShipEntity, IList<ShopSupportInfoEntity>>(basicInfo,
                relationShip, Supports);
        }

        /// <summary>
        ///     从entity 组装成dto
        /// </summary>
        /// <param name="basicInfo"></param>
        /// <param name="extraInfo"></param>
        /// <param name="supportsInfos"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static ShopInfoDto Convert(ShopBasicInfoEntity basicInfo, ShopExtraInfoEntity extraInfo,
            IList<ShopSupportInfoEntity> supportsInfos, string appid)
        {
            var dto = new ShopInfoDto
            {
                ShopId = basicInfo.ShopId,
                Adcode = basicInfo.ShopAdCode,
                Avatar = basicInfo.ShopAvatar,
                Bulletin = basicInfo.ShopBulletin,
                DeliveryPrice = basicInfo.ShopDeliveryPrice,
                Description = basicInfo.ShopDescription,
                DetailAddress = basicInfo.ShopDetailAddress,
                GpsAddress = basicInfo.ShopGpsAddress,
                Infos = basicInfo.ShopInfos.Split(',').ToList(),
                Location = basicInfo.ShopLocation,
                MinPrice = basicInfo.ShopMinOrderMy,
                Name = basicInfo.ShopName,
                Score = basicInfo.ShopScore,
                SellCount = extraInfo?.ShopSellCount ?? 0,
                Supports = supportsInfos?.ToList()??new List<ShopSupportInfoEntity>(),
                ShopAppid = appid,
                IsSendCenter = basicInfo.IsSendCenter,
                IsSinceCenter = basicInfo.IsSinceCenter,
                ShopPhone = basicInfo.ShopPhone??string.Empty
            };

            return dto;
        }

        #region 暂时废弃交互字段

        //[JsonProperty("deliveryTime")] 承诺送达时间
        //public int DeliveryTime { get; set; }
        //[JsonProperty("foodScore")] 食品得分
        //public float FoodScore { get; set; }
        //[JsonProperty("pics")] 店铺图片介绍(多图)
        //public string[] Pics { get; set; }
        //[JsonProperty("ratingCount")] 参与评价的人数
        //public int RatingCount { get; set; }
        //[JsonProperty("serviceScore")] // 服务打分
        //public decimal ServiceScore { get; set; }

        #endregion
    }
}